<template>
  <div class="share">
    <div class="share-return">
      <el-button plain icon="el-icon-back" type="text" @click="returnTo()">返回</el-button>
    </div>
    <div class="share-main">
      <img src="./../assets/logo.png" draggable="false"/>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item prop="url">
          <el-input v-model="form.url" placeholder="分享链接"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" placeholder="分享密码"></el-input>
        </el-form-item>
        <el-form-item prop="get">
          <el-button type="primary" size="medium" @click="getShare()">提取</el-button>
          <template v-if="this.user === null">
            <el-link :underline="false" href="/#/">登录</el-link>
          </template>
        </el-form-item>
      </el-form>
    </div>
    <div class="share-power">
      © 2021 Powered by zsc ccying
    </div>
  </div>
</template>

<style>
  .share-return {
    padding-right: 600px;
  }
  .share-main {
    width: 600px;
    margin: auto;
  }
  .share-power {
    padding-top: 100px;
  }
</style>

<script>
  export default {
    data() {
      let user = sessionStorage.getItem('user');
      return {
        form: {
          url: '',
          password: ''
        },
        user: user
      }
    },

    methods: {
      getShare() {
        let pass = parseInt(this.form.password) + 123456;
        this.$http({
          method: 'post',
          url: '/file/share/verify',
          data: this.$qs.stringify({
            url: this.form.url,
            password: pass
          })
        }).then(res=>{
          let {code:code, data:data} = res.data;
          console.log(data)
          if (code === 200) {
            let type = 1;
            let urls = this.$http.defaults.baseURL + "file/download/" + data + "/" + type;
            window.location.href = urls;
            // window.open(urls, '_blank');
          } else if(code === 3000) {
            alert("分享链接错误")
          } else if (code === 3100) {
            alert("密码错误")
          }
        })
      },
      returnTo() {
        this.$router.go(-1);
      }
    },
  }
</script>
